package com.ng.framework.view.desktop.table;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import com.ng.framework.util.NGUtil;

public class NGTextAreaCellRenderer extends JTextArea implements
		TableCellRenderer {

	private static final long serialVersionUID = 4658076239921896912L;

	private Border noFocusBorder;
	
	private List<List<Integer>> rowColHeight = new ArrayList<List<Integer>>();

	public NGTextAreaCellRenderer() {
		this.noFocusBorder = new EmptyBorder(0, 0, 0, 0);
		this.setOpaque(true);
		this.setBorder(noFocusBorder);
		this.setLineWrap(true);
		this.setWrapStyleWord(true);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		Color foreground = null;
		Color background = null;
		NumberFormat numberFormat = null;
		boolean fraction = false;

		TableModel model = table.getModel();
		if (model instanceof NGTableModel) {
			NGCellAttributes cellAtt = ((NGTableModel) model)
					.getCellAttributes();

			foreground = cellAtt.getCellForeground(row, column);
			background = cellAtt.getCellBackground(row, column);
			fraction = cellAtt.isFractionColumn(column);

			if (value != null) {
				numberFormat = cellAtt.getNumberFormat(value.getClass());
			}
		}

		if (isSelected) {
			this.setForeground((foreground != null) ? foreground : table
					.getSelectionForeground());
			this.setBackground(table.getSelectionBackground());
		} else {
			if (row % 2 == 0) {
				this.setBackground((background != null) ? background
						: new Color(UIManager.getColor(
								"Table.alternateRowColor").getRGB()));
			} else {
				this.setBackground((background != null) ? background
						: new Color(table.getBackground().getRGB()));
			}
			this.setForeground((foreground != null) ? foreground : table
					.getForeground());
		}

		this.setFont(table.getFont());

//		if (hasFocus) {
//			setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
//		} else {
//			setBorder(noFocusBorder);
//		}

		if (fraction) {
			this.setText((value == null) ? "" : NGUtil.doubleToFraction(
					(Double) value, true));
		} else if (numberFormat != null) {
			this.setText(numberFormat.format(value));
		} else {
			this.setText((value == null) ? "" : value.toString());
		}

		 adjustRowHeight(table, row, column);
		

		return this;
	}

	/**
     * Calculate the new preferred height for a given row, and sets the height on the table.
     */
    private void adjustRowHeight(JTable table, int row, int column) {
      int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
      setSize(new Dimension(cWidth, 1000));
      int prefH = getPreferredSize().height;
      while (rowColHeight.size() <= row) {
        rowColHeight.add(new ArrayList<Integer>(column));
      }
      List<Integer> colHeights = rowColHeight.get(row);
      while (colHeights.size() <= column) {
        colHeights.add(0);
      }
      colHeights.set(column, prefH);
      int maxH = prefH;
      for (Integer colHeight : colHeights) {
        if (colHeight > maxH) {
          maxH = colHeight;
        }
      }
      if (table.getRowHeight(row) < maxH) {
        table.setRowHeight(row, maxH);
      }
    }

}
